Implement RULE-8-2-9 - PolymorphicClassTypeExpressionInTypeid#979
Merged
MichaelRFairhurst merged 1 commit intomainfrom Nov 26, 2025
Merged
Conversation
Detects use of polymorphic class type expressions as operands to typeid, which may cause unclear evaluation behavior and potential std::bad_typeid exceptions.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request implements RULE-8-2-9 from MISRA C++ 2023, which detects when the typeid operator is used with expressions of polymorphic class type. The rule aims to prevent unclear evaluation behavior, as typeid expressions with polymorphic types are evaluated at runtime (potentially throwing std::bad_typeid), while non-polymorphic types are evaluated at compile-time.
Key changes:
- Added a new query to detect polymorphic class type expressions used as operands to typeid
- Created the Preconditions1 rule package for this and future related rules
- Integrated the new package into the RuleMetadata system
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| rules.csv | Updated RULE-8-2-9 entry to reference the new Preconditions1 package |
| rule_packages/cpp/Preconditions1.json | New rule package metadata for RULE-8-2-9 with query properties |
| cpp/misra/src/rules/RULE-8-2-9/PolymorphicClassTypeExpressionInTypeid.ql | Main query implementation detecting polymorphic expressions in typeid |
| cpp/misra/test/rules/RULE-8-2-9/test.cpp | Comprehensive test cases covering various scenarios |
| cpp/misra/test/rules/RULE-8-2-9/PolymorphicClassTypeExpressionInTypeid.qlref | Test reference to the query |
| cpp/misra/test/rules/RULE-8-2-9/PolymorphicClassTypeExpressionInTypeid.expected | Expected test results with 6 NON_COMPLIANT findings |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Integration of Preconditions1 package into the RuleMetadata system |
| cpp/common/src/codingstandards/cpp/exclusions/cpp/Preconditions1.qll | Auto-generated package file with query metadata and accessor functions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Detects use of polymorphic class type expressions as operands to typeid, which may cause unclear evaluation behavior and potential std::bad_typeid exceptions.
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
RULE-8-2-9Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.